package _21_多线程._1114_按序打印;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class _1114_按序打印_ReentrantLock {


    private final ReentrantLock lock = new ReentrantLock();

    private final Condition second = lock.newCondition();

    private final Condition three = lock.newCondition();

    int num;

    public _1114_按序打印_ReentrantLock() {
    }

    public void first(Runnable printFirst) throws InterruptedException {
        lock.lock();
        try {
            printFirst.run();
            num = 1;
            second.signal();
        } finally {
            lock.unlock();
        }
    }

    public void second(Runnable printSecond) throws InterruptedException {
        lock.lock();
        try {
            while (num != 1) second.await();
            printSecond.run();
            num = 2;
            three.signal();
        } finally {
            lock.unlock();
        }

    }

    public void third(Runnable printThird) throws InterruptedException {
        lock.lock();
        try {
            while (num != 2) three.await();
            printThird.run();
        } finally {
            lock.unlock();
        }
    }

}
